<?php
namespace Home\Model; 
use Think\Model;
/**
 * 商品列表模型
 */
class ItemModel extends Model
{
public function index($arg=array())
{
     $page=!empty($arg['page'])?intval($arg['page']):1;           
     $typeid=!empty($arg['typeid'])?intval($arg['typeid']):'';
     $brandid=!empty($arg['brandid'])?intval($arg['brandid']):'';
     $keywords=!empty($arg['keywords'])?$arg['keywords']:'';
     $where['status']=1;////在售状态
     $where['areatype']=2;///网购区
    
     if(!empty($typeid)){
      ////判断是否为顶级分类
      $pid=M('type')->where("typeid='%d'",$typeid)->getField("pid");
      if(empty($pid))
      {
        ////获取顶级分类下的所有产品
         $typeids=M('type')->where("pid='%d'",$typeid)->getField("typeid",true);
         $where['typeid']=array('in',$typeids);
      }else
      {
          $where['typeid']=$typeid;
      }
      }

      if(!empty($typeid)&& !empty($brandid))
      {
         $where['typeid']=$typeid;
         $where['brandid']=$brandid;
         
      }
       if(!empty($keywords))
      {  
         $where['itemname|bn']  = array('like', "%$keywords%");
         
      }
        $count=M('item')->where($where)->count();
        $pagearr=getPageArr($page,C('PAGESIZE'),$count);
        $result=M('item')->where($where)->order("itemid desc")->limit($pagearr['start'],$pagearr['pagesize'])->getField("itemid,itemname,bn,itemimg,price");

        $temp['item']=!empty($result)?$result:'';
        $temp['pagearr']=$pagearr;
        
        return $temp;
       

}
}